var ENDPOINT_PATH = "consumer";
layui.extend({
    baseConfig : "/js/pages/baseConfig"
})
layui.use(['form','layer','laydate','table','laytpl','element','util','baseConfig'],function(){

    var form = layui.form
    layer = parent.layer === undefined ? layui.layer : top.layer,
        baseConfig = layui.baseConfig,
        $ = layui.jquery;

    //加载员工列表
    function loadJobNumber() {
        $.get( "/userAdmin/listForSelect",function( res) {
            var users = res;
            var sel = $("select[name=jobNumber]");
            for (var i = 0; i < users.length; i++) {
                var user = users[i];
                if( !user.jobNumber){
                    continue;
                }
                sel.append("<option value=\"" + user.jobNumber + "\">" + user.jobNumber + "</option> ");
                if (i == 0) {
                    loadCooperative(user.jobNumber);
                }
            }
            //更新渲染
            form.render("select");
        });
    };
    //监听员工编号
    form.on('select(jobNumber)', function(item) {
        loadCooperative(item.value);
    });
    loadJobNumber();

    //加载供应商列表
    function loadCooperative(jobNumber) {
        $.get( "/cooperativeNumber/listForSelect?jobNumber="+jobNumber,function( res) {
            var cooperativeNumbers = res.data;
            var sel = $("select[name=fournisseur]");
            sel.empty()
            for (var i = 0; i < cooperativeNumbers.length; i++) {
                var cooperativeNumberModel = cooperativeNumbers[i];
                if( !cooperativeNumberModel.name){
                    continue;
                }
                sel.append("<option value=\"" + cooperativeNumberModel.jobNumber + "\">" + cooperativeNumberModel.name + "</option> ")
            }
            //更新渲染
            form.render("select");
        });
    }

    //绑定校验
    form.verify({
        dateDeDepense : function(val){
            if( !val || val == ''){
                return "请输入日期";
            }
        },
        fournisseur : function(val){
            if( !val || val == ''){
                return "请选择供应商";
            }
        },
        jobNumber : function(val){
            if( !val || val == ''){
                return "请选择工号";
            }
        },
        carteIdentifiant : function(val){
            if(!val || val == ''){
                return "请输入carteIdentifiant";
            }
            var rule = /^[A-Za-z0-9]+$/;
            if( !rule.test( val)){
                return "carteIdentifiant必须为字母或者数字";
            }
        },
        montantTTC : function(val){
            if(!val || val == ''){
                return "请输入montantTTC";
            }
        }
    });

    //监听提交
    form.on("submit(submitBtn)",function( data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        //提交数据
        $.post( "/" + ENDPOINT_PATH + "/addOrUpdate",{
            // id : data.field.id,
            dateDeDepense : data.field.dateDeDepense,
            fournisseur : data.field.fournisseur,
            collaborateur : data.field.jobNumber,
            carteIdentifiant : data.field.carteIdentifiant,
            montantTTC : data.field.montantTTC,
        },function( res){
            if( res.code == 200){
                setTimeout(function(){
                    top.layer.close( index);
                    top.layer.alert( res.msg);
                    layer.closeAll( "iframe");
                    var index1 = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index1);
                    //刷新父页面
                    parent.location.reload();
                },500);
            }else if( res.code == 60001){
                top.layer.close( index);
                top.layer.msg( res.msg);
            }else if( res.code == 60002){
                top.layer.close( index);
                top.layer.msg( res.msg);
            }else{
                top.layer.close( index);
                top.layer.msg( res.msg);
            }
        });
    });
});
